package com.bittrex.bittrex.controller;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import de.elbatya.cryptocoins.bittrexclient.BittrexClient;
import de.elbatya.cryptocoins.bittrexclient.api.model.common.ApiResult;
import de.elbatya.cryptocoins.bittrexclient.api.model.marketapi.OpenOrder;
import de.elbatya.cryptocoins.bittrexclient.api.model.marketapi.OrderCreated;
import de.elbatya.cryptocoins.bittrexclient.config.ApiCredentials;

@RequestMapping("/market")
@RestController
public class BittrexMarketApiController
{

    @Value("${apikey}")
    private String key;

    @Value("${secret}")
    private String secret;

    /**
     * 
     * @创建人: Administrator
     * @方法描述: 用于在特定市场中下单。用于buylimit下限价订单。确保您在API秘钥上设置适当的权限才能使此调用生效。
     * @创建时间: 2018年8月11日 上午11:29:57
     * @param market
     * @param quantity
     * @param rate
     * @return ApiResult<OrderCreated>
     */
    @RequestMapping("/buylimit")
    public ApiResult<OrderCreated> buyLimit(String market, BigDecimal quantity, BigDecimal rate)
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<OrderCreated> apiResult = bittrexClient.getMarketApi().buyLimit(market, quantity, rate);
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }

    /**
     * 
     * @创建人: Administrator
     * @方法描述: 用于在特定市场下达卖单。用于selllimit下限价订单。确保您在API秘钥上设置适当的权限才能使此调用生效。
     * @创建时间: 2018年8月11日 上午11:35:32
     * @param market
     * @param quantity
     * @param rate
     * @return ApiResult<OrderCreated>
     */
    @RequestMapping("/selllimit")
    public ApiResult<OrderCreated> sellLimit(String market, BigDecimal quantity, BigDecimal rate)
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<OrderCreated> apiResult = bittrexClient.getMarketApi().sellLimit(market, quantity, rate);
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }

    /**
     * 
     * @创建人: Administrator
     * @方法描述: 用于取消买入或卖出订单
     * @创建时间: 2018年8月11日 上午11:39:46
     * @param orderUuid
     * @return ApiResult<Void>
     */
    @RequestMapping("/cancel")
    public ApiResult<Void> sellLimit(String orderUuid)
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<Void> apiResult = bittrexClient.getMarketApi().cancelOrder(orderUuid);
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }

    /**
     * 
     * @创建人: Administrator
     * @方法描述: 获取您当前打开的所有订单。可以要求特定的市场
     * @创建时间: 2018年8月11日 上午11:41:14
     * @return ApiResult<List<OpenOrder>>
     */
    @RequestMapping("/getOpenOrders")
    public ApiResult<List<OpenOrder>> getOpenOrders()
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<List<OpenOrder>> apiResult = bittrexClient.getMarketApi().getOpenOrders();
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }
}
